﻿#ifndef RECOGNIZE_H
#define RECOGNIZE_H

#include <QObject>
#include <QNetworkReply>
#include <time.h>
#include <QTimer>
#include "oeasycommon.h"

class QReplyTimeout : public QObject
{
    Q_OBJECT
public:
    QReplyTimeout(QNetworkReply *reply, const int timeout) : QObject(reply)
    {
        Q_ASSERT(reply);
        if (reply && reply->isRunning())
        {
            QTimer::singleShot(timeout, this, SLOT(onTimeout()));
        }
    }

signals:
    void timeout();

private slots:
    void onTimeout()
    {
        QNetworkReply *reply = static_cast<QNetworkReply*>(parent());
        if (reply->isRunning())
        {
            DEBUG_PRINT << "";
            reply->abort();
            reply->deleteLater();
            reply->manager()->deleteLater();
            emit timeout();
        }
    }
};

class Recognize : public QObject
{
    Q_OBJECT
public:
    explicit Recognize(QString id, STRUCT_RECOG_INFO &cfg, QObject *parent = 0);
    ~Recognize();
    void recognize(QByteArray jpg);
    bool m_state;

private:
    QString m_id;
    STRUCT_RECOG_INFO &m_cfg;
    QByteArray m_jpg;
    clock_t m_start;
    QTcpSocket m_sock;
    QNetworkAccessManager *m_recogManager;
    QNetworkReply *m_reply;
    QReplyTimeout *m_replyTimeout;

    void comparison();
    void saveFilterJpg(QString dir, QByteArray jpg);

signals:
    void postOver(QByteArray, QByteArray);

public slots:
    void onDetectOver();
    void onRecogFinished(QNetworkReply*);
};

#endif // RECOGNIZE_H
